package com.googlecode.intellimars.translator;

import com.bitbakery.plugin.translator.TranslatorConfig;
import com.googlecode.intellimars.ui.TabbedToolWindow;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.peer.PeerFactory;
import com.intellij.ui.content.Content;
import org.jetbrains.annotations.NotNull;

public class TranslatorPanelPlugin implements ProjectComponent {
    private static final String TOOL_WINDOW_ID = "TranslatePanel";

    private Project _project;
    private TabbedToolWindow tabbedToolWindow;

    public TranslatorPanelPlugin(Project project) {
        _project = project;
    }

    public void initComponent() { }

    public void disposeComponent() { }

    @NotNull
    public String getComponentName() {
        return "TranslatePlugin";
    }

    public void projectOpened() {
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(_project);
        tabbedToolWindow = new TabbedToolWindow(TOOL_WINDOW_ID, toolWindowManager, true, ToolWindowAnchor.BOTTOM);

        PeerFactory peerFactory = PeerFactory.getInstance();
        TranslatorConfig config = ApplicationManager.getApplication().getComponent(TranslatorConfig.class);
        TranslatorPanel translatePanel;
        Content content;

        translatePanel = new TranslatorPanel();
        translatePanel.init(config);
        content = peerFactory.getContentFactory().createContent(translatePanel.rootPanel, "P:1", false);
        tabbedToolWindow.registerPanel("panel1", content);

        translatePanel = new TranslatorPanel();
        translatePanel.init(config);
        content = peerFactory.getContentFactory().createContent(translatePanel.rootPanel, "P:2", false);
        tabbedToolWindow.registerPanel("panel2", content);

        translatePanel = new TranslatorPanel();
        translatePanel.init(config);
        content = peerFactory.getContentFactory().createContent(translatePanel.rootPanel, "P:3", false);
        tabbedToolWindow.registerPanel("panel3", content);

        tabbedToolWindow.focusPanelIfExists("panel1");
        tabbedToolWindow.startTabChangeListener();
    }

    public void projectClosed() {
        tabbedToolWindow.stopTabChangeListener();
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(_project);
        toolWindowManager.unregisterToolWindow(TOOL_WINDOW_ID);
    }
}
